package nuist.hanniko.stu.webbackend.controller.api;

import nuist.hanniko.stu.webbackend.service.impls.DeviceService;
import nuist.hanniko.stu.webbackend.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/device/record")
public class DeviceRecordController {

    private final DeviceService deviceService;

    @Autowired
    public DeviceRecordController(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @GetMapping("history/{deviceId}")
    @ResponseBody
    public ResponseData getHistoryRecordByDevice(@PathVariable String deviceId,
                                                 @RequestParam(name = "bgTime") Date begin,
                                                 @RequestParam(name = "endTime") Date end) {
        ResponseData responseData = new ResponseData();

        responseData.setData(deviceService.getHistoryRecordByDeviceIdWithinPeriod(deviceId, begin, end));

        return responseData;
    }

    @GetMapping("history/{deviceId}/{objectId}")
    @ResponseBody
    public ResponseData getHistoryRecordByObject(@PathVariable String deviceId,
                                                 @PathVariable String objectId,
                                                 @RequestParam(name = "bgTime") Date begin,
                                                 @RequestParam(name = "endTime") Date end) {
        ResponseData responseData = new ResponseData();

        responseData.setData(
                deviceService.getHistoryRecordByObjetIdWithinPeriod(deviceId, objectId, begin, end));

        return responseData;
    }

    @GetMapping("history/{deviceId}/{objectId}/{resourceId}")
    @ResponseBody
    public ResponseData getHistoryRecordByResource(@PathVariable String deviceId,
                                                   @PathVariable String objectId,
                                                   @PathVariable String resourceId,
                                                   @RequestParam(name = "bgTime") Date begin,
                                                   @RequestParam(name = "endTime") Date end) {
        ResponseData responseData = new ResponseData();

        responseData.setData(
                deviceService.getHistoryRecordByResourceIdWithinPeriod(deviceId, objectId, resourceId, begin, end));

        return responseData;
    }

}
